local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();
local BattleTool=_G.CommonOHOH.load.BattleTool();
local InstanceManager = CommonOHOH.load.InstanceManager();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.car=self.parent
	self.player=self.parent.player
    self.isPlayer = self.parent.isPlayer
    self.isBoom = self.parent.isBoom

      
    local touchPart=self.car:GetWeapon():GetTouchPart() --获取钻头碰撞题
    -- print("### 碰撞监听了",touchPart)
    CallBackManager:AddRbxConnect(self,touchPart.Touched, function(obj,part)
        if part.Name == "CarCollider" then
            if self.car and self.isBoom then
                self.car:CarCrash()
                self.isBoom = false
                task.wait(3);
                self.car.PVPEndEvent:Action()
            end
        end
    end);
 self:Init()
	

end;

function M.AddForceField(self,part)
    local forceField = Instance.new("ForceField")
    forceField.Visible = true 
    forceField.Parent = part
end

function M.RemoveForceField(self,part)
    for _, child in ipairs(part:GetChildren()) do
        if child:IsA("ForceField") then
            child:Destroy()
            -- break -- 找到后可以退出循环
        end
    end
end
function M.Init(self)

    local character = self.player.main_inst
    local car = self.car:GetModel()
    self:RemoveForceField(character)
    self:RemoveForceField(car)

	if not self.isBoom then
        if self.isPlayer  then
           self: AddForceField(character)
        end  
        self: AddForceField(car)

	end
end
-- --根据部件找到矿石
-- function M.FindMine(self,part)

--     local m=part:FindFirstAncestorOfClass("Model")
--     if m then
--         local min= InstanceManager:FindObject(m)
--         if min and min.attribute.IsCarTouch then
--             return min
--         end
--     end
-- end

-- --计算和矿石的战斗结果
-- function M.battleWithMine(self,mine)
--     local mHp=mine:GetHp()
--     local mDamge=mine:GetDamge()
--     local cHp=self.CarInfoCtrl:GetHp()
--     local cDamge=self.CarInfoCtrl:GetAttack()

--    local hurt= BattleTool.GetAttackerHurt({
--         hp=cHp,
--         damge=cDamge,
--     },{
--         hp=mHp,
--         damge=mDamge,
--     })

--     self.CarInfoCtrl:Hurt(hurt)

--     if cHp>hurt then
--         mine:Hurt(mHp,self.car:GetAllPlayer())
--     else
--         self:Dead()
--     end
    
-- end

-- --车子被撞毁
-- function M.Dead(self)
--  --待实现....
-- end


function M.dtor(self)

	M.super.dtor(self);
end


return M